﻿using System;
using System.Text;
using Native.XQ.HIni.Tool;
using Native.XQ.SDK.Event.EventArgs;
using Native.XQ.SDK.Interfaces;

namespace Drawastick.Code
{
    public class Event_GroupMsg : IXQGroupMessage
    {
        public void GroupMessage(object sender, XQAppGroupMsgEventArgs e)
        {
            IniObject dataini = new IniObject(e.XQAPI.AppDirectory + "data.ini");
            IniObject qini = new IniObject(e.XQAPI.AppDirectory + "签.ini");
            dataini.Load();
            qini.Load();
            //抽签 解签
            if (e.Message.Text == "抽签")
            {
                if (qini.Sections.Count <= 0)
                {
                    e.FromGroup.SendMessage(e.RobotQQ, "抽签罐空空如也...\n尝试前往设置界面下载默认签罐吧");
                    return;
                }

                var str = "";
                if (qini["是否开启"]["分群"] == "1")
                {
                    str = dataini[$"{e.FromGroup.Id}-{e.FromQQ.Id}"][DateTime.Today.ToString()];
                }
                else
                {
                    str = dataini[$"{e.FromQQ.Id}"][DateTime.Today.ToString()];
                }

                //判断是否抽过签
                if (str == null || str == "")
                {
                    //没有抽过
                    var qs = qini.Sections;
                    qs = qs.FindAll(s => s.SectionName != "是否开启");

                    var stick = qs[new Random().Next(0, qs.Count)].SectionName;

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine($"[@{e.FromQQ.Id}]");

                    sb.AppendLine($"签位：{stick}");
                    var qwmsg = qini[stick]["签诗"];
                    sb.AppendLine(qwmsg);
                    sb.Append("解签请发送 解签");

                    e.FromGroup.SendMessage(e.RobotQQ, sb.ToString());

                    if (qini["是否开启"]["分群"] == "1")
                    {
                        dataini[$"{e.FromGroup.Id}-{e.FromQQ.Id}"][DateTime.Today.ToString()] = stick;
                    }
                    else
                    {
                        dataini[$"{e.FromQQ.Id}"][DateTime.Today.ToString()] = stick;
                    }
                    dataini.Save();
                }
                else
                {
                    //抽过
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine($"[@{e.FromQQ.Id}]");
                    sb.AppendLine($"您今天已经抽过签了！");
                    sb.AppendLine($"签位：{str}");
                    var qwmsg = qini[str]["签诗"];
                    sb.AppendLine(qwmsg);
                    sb.Append("解签请发送 解签");

                    e.FromGroup.SendMessage(e.RobotQQ, sb.ToString());
                }
            }

            if (e.Message.Text == "解签")
            {
                //判断是否抽过签
                var str = "";
                if (qini["是否开启"]["分群"] == "1")
                {
                    str = dataini[$"{e.FromGroup.Id}-{e.FromQQ.Id}"][DateTime.Today.ToString()];
                }
                else
                {
                    str = dataini[$"{e.FromQQ.Id}"][DateTime.Today.ToString()];
                }
                if (str == null || str == "")
                {
                    //没抽过
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine($"[@{e.FromQQ.Id}]");
                    sb.AppendLine($"您今天还未抽过签！");
                    sb.Append("抽签请发送 抽签");

                    e.FromGroup.SendMessage(e.RobotQQ, sb.ToString());
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine($"[@{e.FromQQ.Id}]");
                    sb.AppendLine($"解签 {str}");
                    var msg = qini[str]["解签"];
                    sb.Append(msg);

                    e.FromGroup.SendMessage(e.RobotQQ, sb.ToString());
                }
            }
        }
    }
}